//
// Created by aaa on 2025/7/10.
//

#include<bits/stdc++.h>

using namespace std;
const int N = 1e4 + 10;
int n,ans;
char a[N],b[N];
int dp[N][N];

//最长公共子串（Longest Common Substring）LCS
//6
//ABACCB
//AACCAB
int main(){
    cin >> n;
    cin >> (a + 1);
    cin >> (b + 1);

    for(int i = 1;i <= n;i++) {
        for(int j = 1;j <= n;j++){
            if(a[i] == b[j]) dp[i][j] = dp[i-1][j-1]+1;
            else dp[i][j] = 0;
            ans = max(ans,dp[i][j]);
        }
    }

    for(int i = 1;i <= n;i++) {
        for(int j = 1;j <= n;j++){
            cout << dp[i][j] << " ";
        }
        cout << endl;
    }

    cout << ans;

    return 0;
}